﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Practices.Unity;
using ScrumTable.BL.DM.DataManagement;
using ScrumTable.Common.Unity;
using ScrumTable.Config.ConfigManagement;
using ScrumTable.Config.ElementManagement;
using ScrumTable.UI.View.WinTouch.UserControls.Tool.Toast;
using ScrumTable.UI.ViewModel;


#endregion

namespace ScrumTable.UI.View.WinTouch.UserControls
{
    /// <summary>
    /// base class for scrum user class
    /// dispose the view-model
    /// </summary>
    public class ScrumUserControl : UserControl, IDisposable
    {

       

        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------
        private bool _disposed = false;
        private static ConfigManager _configManager;
        private static ElementManager _elementManager;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------


        public ConfigManager ConfigManager
        {
            get
            {
                if (_configManager == null)
                {
                    _configManager = ((IAppScreen)Application.Current.MainWindow).Container.Resolve<ConfigManager>();
                }
                return _configManager;
            }
        }

        public ElementManager ElementManager
        {
            get
            {
                if (_elementManager == null)
                {
                    _elementManager = ((IAppScreen)Application.Current.MainWindow).Container.Resolve<ElementManager>();
                }
                return _elementManager;
            }
        }




        /// <summary>
        /// Viewmodel which provides data  for the current ScrumUserControl
        /// </summary>
        /// <value>The view model.</value>
        protected ViewModelBase ViewModel
        {
            set
            {
                DataContext = value;
            }
            get
            {
                if (DataContext is ViewModelBase)
                {
                    return (ViewModelBase)DataContext;
                }
                return null;
            }
        }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------
        /// <summary>
        /// Initializes a new instance of the <see cref="ScrumUserControl"/> class.
        /// </summary>
        public ScrumUserControl()
        {
            KeyboardNavigation.SetTabNavigation(this, KeyboardNavigationMode.Cycle);
        }


        ~ScrumUserControl()
        {
            Dispose(false);
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------


        /// <summary>
        /// Implement dispose interface in order to support runtime support.
        /// </summary>
        public void Dispose()
        {
            Dispose(true);

            // Use SupressFinalize in case a subclass
            // of this type implements a finalizer.
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Disposes the managed and unmanaged resources.
        /// </summary>
        /// <param name="disposing">True to dispose the managed resources, otherwise false.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                ReleaseUnmanagedResources();

                if (disposing)
                    ReleaseManagedResources();

                _disposed = true; // indicate that the instance has been disposed
            }
        }

        /// <summary>
        /// Releases unmanaged resources.
        /// </summary>
        protected virtual void ReleaseUnmanagedResources()
        {
        }

        /// <summary>
        /// Releases managed (c# or .net) resources.
        /// </summary>
        protected virtual void ReleaseManagedResources()
        {
            if (ViewModel != null)
            {
                ViewModel.Dispose();
            }
        }


        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion


        #region BuildUpViewModelSourceAttachProperty
        /// <summary>
        /// Sets the Model from the ScrumUserControl including the depedency injection (Build up)
        /// </summary>
        public static readonly DependencyProperty BuildUpViewModelSourceProperty = DependencyProperty.RegisterAttached(
               "BuildUpViewModelSource",
               typeof(ViewModelBase),
               typeof(ScrumUserControl),
               new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.None, OnMultipleColumnsRowsChanged)
               );

        private static void OnMultipleColumnsRowsChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            SetBuildUpViewModelSource((ScrumUserControl)(sender), args.NewValue as ViewModelBase);
        }

        public static void SetBuildUpViewModelSource(ScrumUserControl element, ViewModelBase value)
        {
            element.ViewModel = (ViewModelBase)((IAppScreen)Application.Current.MainWindow).Container.BuildUp(value.GetType(), value);
        }

        public static ViewModelBase GetBuildUpViewModelSource(ScrumUserControl element)
        {
            return null;
        }

        /// <summary>
        /// Calls BuildUp for the given ScrumUserControl 
        /// </summary>
        /// <param name="element"></param>
        /// <param name="buildUp"></param>
        public static void SetBuildUpElement(ScrumUserControl element, bool buildUp)
        {
            ((IAppScreen)Application.Current.MainWindow).Container.BuildUp(element.GetType(), element);
        }

   
        #endregion
    }
}